//
//  Forum_More_ViewController.m
//  Pacific_Computer
//
//  Created by 范琦 on 14-12-25.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "Forum_More_ViewController.h"
#import "UIColor+AddColor.h"
#import "UIScrollView+Extension.h"
#import "UIScrollView+MJRefresh.h"
#import "Forum_More.h"
#import "Forum_MoreCell.h"
#import "Forum_detail_ViewController.h"
#import "NetHandler.h"

@interface Forum_More_ViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, retain) UITableView *table;
@property(nonatomic, retain) NSMutableArray *arr;
@property(nonatomic, retain) NSMutableArray *arrz;

@end

@implementation Forum_More_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"每日热帖";
    self.view.backgroundColor = [UIColor huiseColor];
    
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(5, 5, self.view.bounds.size.width - 10, self.view.bounds.size.height) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    self.table.rowHeight = 150;
    [self.table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [_table release];
    [self setupRefresh];
    
}
- (void)setupRefresh
{
    //    NSLog(@"进入了setupRefresh");
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    
    [self.table addHeaderWithTarget:self action:@selector(headerRereshing)];
    //进入刷新状态(一进入程序就下拉刷新)
    [self.table headerBeginRefreshing];
    
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.table addFooterWithTarget:self action:@selector(footerRereshing)];
}



#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
    self.arrz = [NSMutableArray array];
    self.arr = [NSMutableArray array];
    //1. 在这调用请求网络数据方法（刷新数据）
    NSString *str = [@"http://itbbs.pconline.com.cn/mobile/topics.ajax?type=hot_day&forums=8,2,240024,41,240022&singleForum=false&noForums=762423,2312647&ie=utf-8&count=20&showImage=false" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [NetHandler getDataWithUrl:str completion:^(NSData *data) {
        id result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSArray *arr = [result objectForKey:@"hot-topics"];
        for (NSDictionary *dic in arr) {
            Forum_More *more = [[Forum_More alloc]init];
            [more setValuesForKeysWithDictionary:dic];
            [self.arrz addObject:more];
            [more release];
        }
        
        for (int i = 0; i < 20; i++) {
            Forum_More *more = [self.arrz objectAtIndex:i];
            [self.arr addObject:more];
        }
        [self.table reloadData];
    }];
    //2. 2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [self.table reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.table headerEndRefreshing];
    });
}

//   进入加载状态
- (void)footerRereshing
{
    //1. 拼接口等操作
    // 请求加载数据
    if (self.arr.count <= 99) {
        NSInteger a = self.arr.count;
        if (a == 80) {
            for (NSInteger i = a ; i < a + 19; i++) {
                Forum_More *more = [self.arrz objectAtIndex:i];
                [self.arr addObject:more];
            }
            
        }else{
            
            for (NSInteger i = a ; i < a + 20; i++) {
                Forum_More *more = [self.arrz objectAtIndex:i];
                [self.arr addObject:more];
            }
        }
    }
    
    
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.table reloadData];
        [self.table footerEndRefreshing];
    });
}






- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Forum_MoreCell *cell = [tableView dequeueReusableCellWithIdentifier:@"a"];
    if (cell == nil) {
        cell = [[Forum_MoreCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"a"];
    }
    Forum_More *more = [self.arr objectAtIndex:indexPath.row];
    cell.title.text = more.title;
    cell.message.text = more.message;
    cell.type.text = more.forumName;
    NSString *str = [NSString stringWithFormat:@"%@阅/%@楼", more.viewCount, more.floor];
    cell.floor.text = str;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    Forum_detail_ViewController *detail = [[Forum_detail_ViewController alloc]init];
    Forum_More *more = [self.arr objectAtIndex:indexPath.row];
    detail.more = more;
    [self.navigationController pushViewController:detail animated:YES];
    [detail release];


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
